<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
<title>用户管理</title>
<link rel="stylesheet" type="text/css" href="${ctx}/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${ctx}/easyui/easyui_my.css">
<link rel="stylesheet" type="text/css" href="${ctx}/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="${ctx}/css/css.css" />

<script type="text/javascript" src="${ctx}/js/jquery-1.7.min.js"></script>
<script type="text/javascript" src="${ctx}/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${ctx }/easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="${ctx}/js/common.js"></script>
<%-- <script type="text/javascript" src="${ctx}/js/easyUIOpenWindow.js"></script> --%>

</head>
<body class="easyui-layout" options="border:false">
	<div data-options="region:'north',height:72,border:false" style="padding-left:10px;padding-right: 10px;padding-top: 5px;">
		<div class="xinxichaxunkuangz1">
			<div class="dashujtiaojianxuanxiangkuan">
				<div class="dashujtiaojianxuanxiangkuan_lie">
						<div class="tiaojian_key">用户信息：</div>
						<div class="tiaojian_values">
							<ul class="tiaojian_values_limian">
								<li><input id="ridSelect" name="ridSelect" value=""/>
								</li>
								<li>
									<input id="ztSelect" name="ztSelect" value="" class="easyui-combobox inputclass" 
										data-options="valueField: 'id',
											textField: 'text', 
											editable:false,
											panelHeight:150,
											data:[{id:'',text:'角色状态'},
											{id:'1',text:'启用'},
											{id:'2',text:'锁定'},
											{id:'3',text:'停用'}]"/>
								</li>
								<li>
									<input id="glbmSelect" name="glbmSelect" value="" style="width: 200px"/>
								</li>
								<li><input id="yhSelect" name="yhSelect" type="text" class="shurukuangkais" value="用户编号或姓名" onblur="InputBlur('yhSelect','用户编号或姓名')" onfocus="InputFocus('yhSelect','用户编号或姓名')" /></li>
							</ul>
						</div>
				</div>
				<div class="chaxunchongzhianniu">
					<a href="javascript:AddWindow()" class="sousuocx">添加</a>
					<a href="javascript:deleteUsers()" class="congzhim">删除</a>
					<a href="javascript:queryDataGrid()" class="congzhim">查询</a>
				</div>
			</div>
		</div>
	</div>
	<div data-options="region:'center',border:false" style="padding-left:10px;padding-right: 10px">
		<div class="easyui-layout xinxichaxunkuangz2" data-options="fit:true,border:false">
			<div class="tupianqiehuantop" data-options="region:'north',border:false" style="height: 40px">
              	<span class="chaxunjieguosd">搜索结果</span>
     		</div>
     		<div data-options="region:'center',border:false">
     			<table id="TableData" data-options="fit:true"></table>
     		</div>
     		<jsp:include page="/WEB-INF/content/user/addOrUpdateUser.jsp" />
		</div>
	</div>
	<script type="text/javascript">
	
		$(function(){
			initTable();
			initRole();
			initGlbm();
			//初始化Ajax表单提交
			$("#saveOrUpdateDevForm").form({
				url:'${ctx }/user/saveUser.htm',
				onSubmit:function(){//提交前校验
					return true;
				},
				success:function(data){//ajax提交后获取数据库返回信息
					if(data == 'true'){
						cancelUpdateDev();
						$("#TableData").datagrid("load");
					}else{
						$.messager.show({
							title:'操作失败',
							msg:'操作失败，请联系管理人员',
							timeout:5000,
							showType:'slide'
						});
					}
				}
			});
			
			
		});
		//数据表
		function initTable(){
			$("#TableData").datagrid({
				url:'${ctx}/user/findUserForPage.htm',
				idField:'yhdh',
				fitColumns:true,
				border:false,
				striped:true,
				//singleSelect:true,
				pagination: true,
				pageSize: 10,                //每页记录数      
	           	pageList: [5, 10, 15, 20, 30, 50], //分页记录数数组 
// 				queryParams:{
// 					pageSize:10
// 					,page:1
// 				},
				columns:[[
					{field:'',checkbox:true,align:'center',split:true,fixed:true},
					{field:'yhdh',title:'用户帐号',width:'100',align:'center',split:true,fixed:true},
				  	{field:'xm',title:'姓名',width:'100',align:'center',split:true,fixed:true},
				  	{field:'sfzmhm',title:'身份证号',width:'150',align:'center',split:true,fixed:true},
				  	{field:'bmmc',title:'所属部门',width:'200',align:'center',split:true,fixed:true},
				  	{field:'rname',title:'角色',width:'120',align:'center',split:true,fixed:true},
				  	{field:'zhyxqStr',title:'帐号有效期',width:'130',align:'center',split:true,fixed:true},
				  	{field:'scdlip',title:'上次登录IP',width:'100',align:'center',split:true,fixed:true},
				  	{field:'scdlsjStr',title:'上次登录时间',width:'130',align:'center',split:true,fixed:true},
				  	{field:'cjr',title:'创建人',width:'100',align:'center',split:true,fixed:true},
				  	{field:'zt',title:'账户状态',width:'100',split:true,fixed:true,formatter:function(value,row,index){
				  		if(row.zt==1){
				  			return "启用";
				  		}else if(row.zt==2){
				  			return "锁定";
				  		}else if(row.zt==3){
				  			return "停用";
				  		}else{return "";}
				  	}},
					{field:'cz',title:'操作',width:'100',align:'center',split:true,fixed:true,formatter:function(value,row,index){
				  		return "<a class=\"chense\" href=\"javascript:UpdateUserInfo('"+row.yhdh+"')\" >修改</a>&nbsp;&nbsp;&nbsp;<a class=\"chense\" href=\"javascript:deleteUser('"+row.yhdh+"')\" >删除</a>";
				  	}}
				]]
			});
		}
		
		//初始化角色下拉框
		function initRole(){
			$.ajax({
				type:'POST',
				url:'${ctx}/role/findRoleAll.htm',
				dataType:'json',
				success:function(data, textStatus, jqXHR){
					data.unshift({//在data首部添加一个元素
						'rid':'',
						'rname':'角色名称'
					});
					$("#ridSelect").combobox({
						valueField: 'rid'
						,textField: 'rname'
						,editable:false
						,panelHeight:150
						,data:data
						,onLoadSuccess: function(data) {
						}
					});
				}
			});
		}
		//初始化管理部门下拉框
		function initGlbm(){
			$("#glbmSelect").combotree({
				editable:false,
				method:'post',
				panelHight:250,
				panelWidth:230,
				url:'${ctx}/department/queryDepartmentTree.htm',
				onLoadSuccess: function(data) {
					$('#glbmSelect').combotree('setValue', '${user.glbm}');
				}
			});
		}
		//根据界面中的样件，过滤查询用户列表
		function queryDataGrid(){
			var yhSelect = $("#yhSelect").val() == "用户编号或姓名" ? "":$("#yhSelect").val();
			var ridSelect = $("input[name='ridSelect']").val();
			var ztSelect = $("input[name='ztSelect']").val();
			var glbmSelect = $("input[name='glbmSelect']").val();
			$("#TableData").datagrid("load",{
				yhdh:yhSelect
				,rid:ridSelect
				,zt:ztSelect
				,glbm:glbmSelect
			});
		}
		
		//弹出用户对话框
		function UpdateUserInfo(yhdh){
			if(yhdh=="admin"){
				if('${user.yhdh}'!="admin"){
					alert("您没有权限修改！");
					return;
				}
			}
			$('#saveOrUpdateDevForm').form('clear');
			$("#userImage").css('display','none');
			$("#saveLx").attr("value","xg");
			$("#yhdh").attr("readonly","readonly");
			$('#SaveOrUpdateDevWin').window('open');
			$.ajax({
				type:'POST',
				url:'${ctx}/user/findUserByYhdh.htm',
				data:{yhdh:yhdh},
				dataType:'json',
				success:function(data, textStatus, jqXHR){
					if(data.zp!=""){
						$("#userImage").css('display','block');
						$("#yhImage").attr("src",'${ctx}/upload/userpic/'+data.zp);
					}
					$('#saveOrUpdateDevForm').form('load',data);
				}
			});
			//$('#saveOrUpdateDevForm').form('load','${ctx}/user/findUserByYhdh2.htm?yhdh='+yhdh+"&timesampe="+new Date().getTime());
		}
		
		//弹出新增用户对话框
		function AddWindow(){
			$('#saveOrUpdateDevForm').form('clear');
			$("#userImage").css('display','none');
			$("#saveLx").attr("value","");
			$("#yhdh").removeAttr("readonly");
			$("#zt").combobox("select", "1");
			$('#SaveOrUpdateDevWin').window('open');
		}
		
		//保存用户信息
		function updateDev(){
			var f=0;
			var a = new Array();
			a.push("yhdh|用户帐号");
			a.push("xm|用户姓名");
			a.push("yhmm|登录密码");
			for(var i=0;i<a.length;i++){
				var id=a[i].split("|")[0];
				var name=a[i].split("|")[1];				
				//if($("#"+id).val()==""){
				if($("#"+id).val()==""){
					f=1;
					alert(name+"不能为空！");
					return;
				}
			}
			if(!yhdhIsValid($("#yhdh").val())){ //验证用户名
				alert("用户账户由字母、数字、下划线组成不超过32个字符！由汉字组成不超过16个汉字！");
				f=1;
				return;
			}
			if(!lengthIsValid($("#yhdh").val())){//验证用户账户长度
				alert("用户账户由字母、数字、下划线组成不超过32个字符！由汉字组成不超过16个汉字！");
				f=1;
				return;
			}
			if(!lengthIsValid($("#mm").val())){//验证密码长度
				alert("密码长度过长！");
				f=1;
				return;
			}
			if(!lengthIsValid($("#xm").val())){//验证姓名长度
				alert("用户姓名过长！");
				f=1;
				return;
			}
			if($("input[name='glbm']").val()==""){//验证部门
				alert("请选择所属部门！");
				f=1;
				return;
			}
			if($("#ipks").val()!=""){//验证ip
				if(!ipIsValid($("#ipks").val())){
					alert("开始的IP格式不正确！");
					f=1;
					return;
				}
			}
			if($("#ipjs").val()!=""){//验证ip
				if(!ipIsValid($("#ipjs").val())){
					alert("结束的IP格式不正确！");
					f=1;
					return;
				}
			}
			if($("#sfzmhm").val()!=""){
				if(!sfzmhmIsValid($("#sfzmhm").val())){//验证身份证
					alert("身份证号码格式不正确！");
					f=1;
					return;
				}
			}
			if($("#saveLx").val()==""){
				if(f==0){
					$.ajax({
						type:'POST',
						url:'${ctx}/user/findUserByYhdh.htm',
						data:{yhdh:$('#yhdh').val()},
						dataType:'json',
						success:function(data,textStatus,jqXHR){
							if(!data==""){
								alert("用户帐号已存在！！");
							}else{
								$("#saveOrUpdateDevForm").submit();
							}
						}
					});
				}
			}else{
				$("#saveOrUpdateDevForm").submit();
			}
		}
		
		//取消保存用户
		function cancelUpdateDev(){
			$("#saveOrUpdateDevForm")[0].reset();
			$("#SaveOrUpdateDevWin").window("close");
		}
		
		//删除用户信息
		function deleteUser(ids){
			if(ids=="admin"){
				alert("admin用户不允许删除！");
				return;
			}else{
				var r=confirm("确认要删除用户信息吗？删除后将不可恢复！");
				if(r){
					$.ajax({
						url:'${ctx}/user/delUser.htm',
						type:'POST',
						data:{
							ids:ids
						},
						success:function(data){
							if(data =="true"){
								queryDataGrid();
							}else{
								$.messager.show({
									title:'操作失败',
									msg:'操作失败，请联系管理人员',
									timeout:5000,
									showType:'slide'
								});
							}
						}
					});
				}
			}
		}
		//删除用户信息
		function deleteUsers(){
			var ids = $("#TableData").datagrid("getSelections");//getSelections
			var selectValues = "";
			var f=0;//用于判断删除数据中是否有admin
			for(var i=0;i<ids.length;i++){
				if(ids[i].yhdh=='admin'){
					f=1;
					alert("admin用户不允许删除！");
					break;
				}else{
					if(i==0){
						selectValues+=ids[i].yhdh;
					}else{
						selectValues+=","+ids[i].yhdh;
					}
				}
			}
			if(f==1){
				return;
			}
			if(selectValues ==""){
				alert("请先选择要删除的记录！");
				return;
			}
			var r=confirm("确认要删除用户信息吗？删除后将不可恢复！");
			if(r){
				$.ajax({
					url:'${ctx}/user/delUser.htm',
					type:'POST',
					data:{
						ids:selectValues
					},
					success:function(data){
						if(data =="true"){
							queryDataGrid();
						}else{
							$.messager.show({
								title:'操作失败',
								msg:'操作失败，请联系管理人员',
								timeout:5000,
								showType:'slide'
							});
						}
					}
				});
			}
		}
		function ipIsValid(ip){
			var regex = new RegExp("\^((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\$");   
			return regex.test(ip);
		}
		function yhdhIsValid(yhdh){
			var regex=new RegExp("\^[_a-zA-Z0-9\u4e00-\u9fa5]{1,32}\$");
			return regex.test(yhdh);
		}
		function lengthIsValid(str){
			var strlength= str.replace(/[^x00-xff]/g,"01").length;
			return strlength<=32;
		}
		function sfzmhmIsValid(sfzmhm){ 
			var regex15=new RegExp("\^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}\$");
			var regex18=new RegExp("\^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)\$");
			return regex15.test(sfzmhm)||regex18.test(sfzmhm);
		}
	</script>
</body>
</html>